<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Command-module to control Windows Explorer &mdash; Dragonfly-modules v0.1 documentation</title>
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '0.1',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="top" title="Dragonfly-modules v0.1 documentation" href="index.html" />
    <link rel="next" title="Command-module for styles in Microsoft Word" href="mod-_word_styles.html" />
    <link rel="prev" title="Command-module for UltraEdit editor" href="mod-uedit32.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="mod-_word_styles.html" title="Command-module for styles in Microsoft Word"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="mod-uedit32.html" title="Command-module for UltraEdit editor"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">Dragonfly-modules v0.1 documentation</a> &raquo;</li> 
      </ul>
    </div>  
    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  
  <div class="section" id="command-module-to-control-windows-explorer">
<h1>Command-module to control <strong>Windows Explorer</strong><a class="headerlink" href="#command-module-to-control-windows-explorer" title="Permalink to this headline">¶</a></h1>
<p>This module defines various voice-commands for use with Windows Explorer.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This module is still under development.</p>
</div>
<div class="section" id="installation">
<h2>Installation<a class="headerlink" href="#installation" title="Permalink to this headline">¶</a></h2>
<p>If you are using DNS and Natlink, simply place this file in you Natlink
macros directory.  It will then be automatically loaded by Natlink when
you next toggle your microphone or restart Natlink.</p>
</div>
<div class="section" id="module-source-code">
<h2>Module source code<a class="headerlink" href="#module-source-code" title="Permalink to this headline">¶</a></h2>
<p>The most current version of this module can be downloaded from the <a class="reference external" href="http://dragonfly-modules.googlecode.com/svn/trunk/command-modules/_explorer_tools.py">repository here</a>.</p>
<div class="highlight-python"><table class="highlighttable"><tr><td class="linenos"><pre>  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246</pre></td><td class="code"><div class="highlight"><pre><span class="c">#</span>
<span class="c"># This file is a command-module for Dragonfly.</span>
<span class="c"># (c) Copyright 2008 by Christo Butcher</span>
<span class="c"># Licensed under the LGPL, see &lt;http://www.gnu.org/licenses/&gt;</span>
<span class="c">#</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Command-module to control **Windows Explorer**</span>
<span class="sd">============================================================================</span>

<span class="sd">This module defines various voice-commands for use with Windows Explorer.</span>

<span class="sd">.. note::</span>

<span class="sd">   This module is still under development.</span>

<span class="sd">Installation</span>
<span class="sd">----------------------------------------------------------------------------</span>

<span class="sd">If you are using DNS and Natlink, simply place this file in you Natlink</span>
<span class="sd">macros directory.  It will then be automatically loaded by Natlink when</span>
<span class="sd">you next toggle your microphone or restart Natlink.</span>

<span class="sd">&quot;&quot;&quot;</span>

<span class="k">try</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">pkg_resources</span>
    <span class="n">pkg_resources</span><span class="o">.</span><span class="n">require</span><span class="p">(</span><span class="s">&quot;dragonfly &gt;= 0.6.5beta1.dev-r76&quot;</span><span class="p">)</span>
<span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
    <span class="k">pass</span>

<span class="kn">import</span> <span class="nn">os.path</span>
<span class="kn">import</span> <span class="nn">string</span>
<span class="kn">import</span> <span class="nn">subprocess</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">from</span> <span class="nn">urllib</span> <span class="kn">import</span> <span class="n">unquote</span>
<span class="kn">from</span> <span class="nn">dragonfly</span> <span class="kn">import</span> <span class="p">(</span><span class="n">ConnectionGrammar</span><span class="p">,</span> <span class="n">AppContext</span><span class="p">,</span> <span class="n">CompoundRule</span><span class="p">,</span>
                       <span class="n">Choice</span><span class="p">,</span> <span class="n">Window</span><span class="p">,</span> <span class="n">Config</span><span class="p">,</span> <span class="n">Section</span><span class="p">,</span> <span class="n">Item</span><span class="p">)</span>


<span class="c">#---------------------------------------------------------------------------</span>

<span class="k">class</span> <span class="nc">SingleFile</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">spec</span><span class="p">,</span> <span class="n">command_line</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">spec</span> <span class="o">=</span> <span class="n">spec</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">command_line</span> <span class="o">=</span> <span class="n">command_line</span>

    <span class="k">def</span> <span class="nf">execute</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">paths</span><span class="p">,</span> <span class="n">directory</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">path</span> <span class="ow">in</span> <span class="n">paths</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">execute_single</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">directory</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">execute_single</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">directory</span><span class="p">):</span>
        <span class="n">data</span> <span class="o">=</span> <span class="p">{</span><span class="s">&quot;path&quot;</span><span class="p">:</span> <span class="n">path</span><span class="p">,</span> <span class="s">&quot;dir&quot;</span><span class="p">:</span> <span class="n">directory</span><span class="p">}</span>
        <span class="n">arguments</span> <span class="o">=</span> <span class="p">[</span><span class="n">s</span> <span class="o">%</span> <span class="n">data</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">command_line</span><span class="p">]</span>
        <span class="k">print</span> <span class="s">&quot;Arguments: </span><span class="si">%r</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">arguments</span>
        <span class="n">process</span> <span class="o">=</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">Popen</span><span class="p">(</span><span class="n">arguments</span><span class="p">,</span> <span class="n">stdout</span><span class="o">=</span><span class="n">subprocess</span><span class="o">.</span><span class="n">PIPE</span><span class="p">)</span>
        <span class="n">out</span><span class="p">,</span> <span class="n">err</span> <span class="o">=</span> <span class="n">process</span><span class="o">.</span><span class="n">communicate</span><span class="p">()</span>
        <span class="k">print</span> <span class="s">&quot;Output:&quot;</span><span class="p">;</span> <span class="k">print</span> <span class="n">out</span> <span class="ow">and</span> <span class="n">out</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
        <span class="k">print</span> <span class="s">&quot;Error:&quot;</span><span class="p">;</span> <span class="k">print</span> <span class="n">err</span> <span class="ow">and</span> <span class="n">err</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>


<span class="k">class</span> <span class="nc">MultiFile</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">spec</span><span class="p">,</span> <span class="n">command_line_pre</span><span class="p">,</span> <span class="n">command_line_post</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">spec</span> <span class="o">=</span> <span class="n">spec</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">command_line_pre</span>  <span class="o">=</span> <span class="n">command_line_pre</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">command_line_post</span> <span class="o">=</span> <span class="n">command_line_post</span>

    <span class="k">def</span> <span class="nf">execute</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">paths</span><span class="p">,</span> <span class="n">directory</span><span class="p">):</span>
        <span class="n">data</span> <span class="o">=</span> <span class="p">{</span><span class="s">&quot;dir&quot;</span><span class="p">:</span> <span class="n">directory</span><span class="p">}</span>
        <span class="n">arguments_pre</span>  <span class="o">=</span> <span class="p">[</span><span class="n">s</span> <span class="o">%</span> <span class="n">data</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">command_line_pre</span><span class="p">]</span>
        <span class="n">arguments_post</span> <span class="o">=</span> <span class="p">[</span><span class="n">s</span> <span class="o">%</span> <span class="n">data</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">command_line_post</span><span class="p">]</span>
        <span class="n">arguments</span> <span class="o">=</span> <span class="n">arguments_pre</span> <span class="o">+</span> <span class="n">paths</span>
        <span class="k">if</span> <span class="n">arguments_post</span><span class="p">:</span>
            <span class="n">arguments</span> <span class="o">+=</span> <span class="n">arguments_post</span>

        <span class="k">print</span> <span class="s">&quot;Arguments: </span><span class="si">%r</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">arguments</span>
        <span class="n">process</span> <span class="o">=</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">Popen</span><span class="p">(</span><span class="n">arguments</span><span class="p">,</span> <span class="n">stdout</span><span class="o">=</span><span class="n">subprocess</span><span class="o">.</span><span class="n">PIPE</span><span class="p">)</span>
        <span class="n">out</span><span class="p">,</span> <span class="n">err</span> <span class="o">=</span> <span class="n">process</span><span class="o">.</span><span class="n">communicate</span><span class="p">()</span>
        <span class="k">print</span> <span class="s">&quot;Output:&quot;</span><span class="p">;</span> <span class="k">print</span> <span class="n">out</span> <span class="ow">and</span> <span class="n">out</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
        <span class="k">print</span> <span class="s">&quot;Error:&quot;</span><span class="p">;</span> <span class="k">print</span> <span class="n">err</span> <span class="ow">and</span> <span class="n">err</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>


<span class="k">class</span> <span class="nc">CreateArchiveHere</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">spec</span><span class="p">,</span> <span class="n">extension</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">spec</span> <span class="o">=</span> <span class="n">spec</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">extension</span> <span class="o">=</span> <span class="n">extension</span>

    <span class="k">def</span> <span class="nf">execute</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">paths</span><span class="p">,</span> <span class="n">directory</span><span class="p">):</span>
        <span class="n">archive_path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">splitext</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">paths</span><span class="p">[</span><span class="mf">0</span><span class="p">]))[</span><span class="mf">0</span><span class="p">]</span>
        <span class="n">archive_path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">directory</span><span class="p">,</span> <span class="n">archive_path</span><span class="p">)</span>
        <span class="n">archive_path</span> <span class="o">+=</span> <span class="n">time</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s">&quot;-%y%m</span><span class="si">%d</span><span class="s">&quot;</span><span class="p">)</span>

        <span class="k">def</span> <span class="nf">filenames</span><span class="p">(</span><span class="n">basename</span><span class="p">,</span> <span class="n">extension</span><span class="p">):</span>
<span class="c">#            yield basename + extension</span>
            <span class="k">for</span> <span class="n">letter</span> <span class="ow">in</span> <span class="n">string</span><span class="o">.</span><span class="n">lowercase</span><span class="p">:</span>
                <span class="k">yield</span> <span class="n">basename</span> <span class="o">+</span> <span class="n">letter</span> <span class="o">+</span> <span class="n">extension</span>

        <span class="n">available</span> <span class="o">=</span> <span class="bp">False</span>
        <span class="k">for</span> <span class="n">archive_path</span> <span class="ow">in</span> <span class="n">filenames</span><span class="p">(</span><span class="n">archive_path</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">extension</span><span class="p">):</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">archive_path</span><span class="p">):</span>
                <span class="n">available</span> <span class="o">=</span> <span class="bp">True</span>
                <span class="k">break</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">available</span><span class="p">:</span>
            <span class="k">print</span> <span class="s">&quot;Warning: could not create archive.&quot;</span>
            <span class="k">return</span>

        <span class="n">arguments</span> <span class="o">=</span> <span class="p">[</span><span class="s">r&quot;C:\Program Files\7-Zip\7z.exe&quot;</span><span class="p">,</span> <span class="s">&quot;a&quot;</span><span class="p">]</span>
        <span class="n">arguments</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&quot;-o&quot;</span> <span class="o">+</span> <span class="n">directory</span><span class="p">)</span>
        <span class="n">arguments</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">archive_path</span><span class="p">)</span>
<span class="c">#        arguments.append(os.path.splitext(archive_path)[0])</span>
        <span class="n">arguments</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">paths</span><span class="p">)</span>

        <span class="k">print</span> <span class="s">&quot;Arguments: </span><span class="si">%r</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">arguments</span>
        <span class="n">process</span> <span class="o">=</span> <span class="n">subprocess</span><span class="o">.</span><span class="n">Popen</span><span class="p">(</span><span class="n">arguments</span><span class="p">,</span> <span class="n">stdout</span><span class="o">=</span><span class="n">subprocess</span><span class="o">.</span><span class="n">PIPE</span><span class="p">)</span>
        <span class="n">out</span><span class="p">,</span> <span class="n">err</span> <span class="o">=</span> <span class="n">process</span><span class="o">.</span><span class="n">communicate</span><span class="p">()</span>
        <span class="k">print</span> <span class="s">&quot;Output:&quot;</span><span class="p">;</span> <span class="k">print</span> <span class="n">out</span> <span class="ow">and</span> <span class="n">out</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
        <span class="k">print</span> <span class="s">&quot;Error:&quot;</span><span class="p">;</span> <span class="k">print</span> <span class="n">err</span> <span class="ow">and</span> <span class="n">err</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>

<span class="k">class</span> <span class="nc">RenameFile</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>

    <span class="n">python_path</span> <span class="o">=</span> <span class="s">r&quot;C:\Python25\python.exe&quot;</span>
    <span class="n">rename_dialog_path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">__file__</span><span class="p">),</span>
                                      <span class="s">&quot;dialog_rename.py&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">spec</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">spec</span> <span class="o">=</span> <span class="n">spec</span>

    <span class="k">def</span> <span class="nf">execute</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">paths</span><span class="p">,</span> <span class="n">directory</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">paths</span><span class="p">)</span> <span class="o">==</span> <span class="mf">0</span><span class="p">:</span>
            <span class="k">print</span> <span class="s">&quot;Rename file error: nothing selected.&quot;</span>
            <span class="k">return</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">paths</span><span class="p">[</span><span class="mf">0</span><span class="p">]</span>

        <span class="n">arguments</span> <span class="o">=</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">python_path</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">rename_dialog_path</span><span class="p">,</span> <span class="n">path</span><span class="p">]</span>
        <span class="n">subprocess</span><span class="o">.</span><span class="n">Popen</span><span class="p">(</span><span class="n">arguments</span><span class="p">)</span>


<span class="c">#---------------------------------------------------------------------------</span>

<span class="n">commands</span> <span class="o">=</span> <span class="p">[</span>
            <span class="n">SingleFile</span><span class="p">(</span><span class="s">&quot;open with ultra [edit]&quot;</span><span class="p">,</span>
                       <span class="p">[</span><span class="s">r&quot;C:\Program Files\IDM Computer Solutions\UltraEdit\Uedit32.exe&quot;</span><span class="p">,</span> <span class="s">&quot;</span><span class="si">%(path)s</span><span class="s">&quot;</span><span class="p">]),</span>
            <span class="n">MultiFile</span><span class="p">(</span><span class="s">&quot;scan for (virus | viruses) | virus scan&quot;</span><span class="p">,</span>
                       <span class="p">[</span><span class="s">r&quot;C:\Program Files\F-Secure\Anti-Virus\fsav.exe&quot;</span><span class="p">],</span> <span class="p">[</span><span class="s">&quot;/list&quot;</span><span class="p">]),</span>
            <span class="n">SingleFile</span><span class="p">(</span><span class="s">&quot;extract archive here&quot;</span><span class="p">,</span>
                       <span class="p">[</span><span class="s">r&quot;C:\Program Files\7-Zip\7z.exe&quot;</span><span class="p">,</span> <span class="s">&quot;x&quot;</span><span class="p">,</span> <span class="s">&quot;-o</span><span class="si">%(dir)s</span><span class="s">&quot;</span><span class="p">,</span> <span class="s">&quot;</span><span class="si">%(path)s</span><span class="s">&quot;</span><span class="p">]),</span>
            <span class="n">CreateArchiveHere</span><span class="p">(</span><span class="s">&quot;create zip archive here&quot;</span><span class="p">,</span> <span class="s">&quot;.zip&quot;</span><span class="p">),</span>
            <span class="n">CreateArchiveHere</span><span class="p">(</span><span class="s">&quot;create [7] archive here&quot;</span><span class="p">,</span> <span class="s">&quot;.7z&quot;</span><span class="p">),</span>
            <span class="n">RenameFile</span><span class="p">(</span><span class="s">&quot;rename file dialog&quot;</span><span class="p">),</span>
           <span class="p">]</span>


<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># Utility generator function for iterating over COM collections.</span>

<span class="k">def</span> <span class="nf">collection_iter</span><span class="p">(</span><span class="n">collection</span><span class="p">):</span>
    <span class="k">for</span> <span class="n">index</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="n">collection</span><span class="o">.</span><span class="n">Count</span><span class="p">):</span>
        <span class="k">yield</span> <span class="n">collection</span><span class="o">.</span><span class="n">Item</span><span class="p">(</span><span class="n">index</span><span class="p">)</span>


<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># This module&#39;s main grammar.</span>

<span class="k">class</span> <span class="nc">ControlGrammar</span><span class="p">(</span><span class="n">ConnectionGrammar</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">ConnectionGrammar</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span>
            <span class="n">name</span><span class="o">=</span><span class="s">&quot;Explorer control&quot;</span><span class="p">,</span>
            <span class="n">context</span><span class="o">=</span><span class="n">AppContext</span><span class="p">(</span><span class="n">executable</span><span class="o">=</span><span class="s">&quot;explorer&quot;</span><span class="p">),</span>
            <span class="n">app_name</span><span class="o">=</span><span class="s">&quot;Shell.Application&quot;</span>
           <span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_active_explorer</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">handle</span> <span class="o">=</span> <span class="n">Window</span><span class="o">.</span><span class="n">get_foreground</span><span class="p">()</span><span class="o">.</span><span class="n">handle</span>
        <span class="k">for</span> <span class="n">window</span> <span class="ow">in</span> <span class="n">collection_iter</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">application</span><span class="o">.</span><span class="n">Windows</span><span class="p">()):</span>
            <span class="k">if</span> <span class="n">window</span><span class="o">.</span><span class="n">HWND</span> <span class="o">==</span> <span class="n">handle</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">window</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_log</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s">: no active explorer.&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">None</span>

    <span class="k">def</span> <span class="nf">get_selected_paths</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">window</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_active_explorer</span><span class="p">()</span>
        <span class="n">items</span> <span class="o">=</span> <span class="n">window</span><span class="o">.</span><span class="n">Document</span><span class="o">.</span><span class="n">SelectedItems</span><span class="p">()</span>
        <span class="n">paths</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">collection_iter</span><span class="p">(</span><span class="n">items</span><span class="p">):</span>
            <span class="n">paths</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">item</span><span class="o">.</span><span class="n">Path</span><span class="p">)</span>
        <span class="k">print</span> <span class="s">&quot;Selected paths: </span><span class="si">%r</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">paths</span>
        <span class="k">return</span> <span class="n">paths</span>

    <span class="k">def</span> <span class="nf">get_selected_filenames</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">paths</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_selected_paths</span><span class="p">()</span>
        <span class="k">return</span> <span class="p">[</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">basename</span><span class="p">(</span><span class="n">p</span><span class="p">)</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">paths</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">get_current_directory</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">window</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_active_explorer</span><span class="p">()</span>
        <span class="n">path</span> <span class="o">=</span> <span class="n">window</span><span class="o">.</span><span class="n">LocationURL</span><span class="p">[</span><span class="mf">8</span><span class="p">:]</span>
        <span class="k">if</span> <span class="n">path</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&quot;file:///&quot;</span><span class="p">):</span>
            <span class="n">path</span> <span class="o">=</span> <span class="n">path</span><span class="p">[</span><span class="mf">8</span><span class="p">:]</span>
        <span class="k">return</span> <span class="n">unquote</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>

<span class="n">grammar</span> <span class="o">=</span> <span class="n">ControlGrammar</span><span class="p">()</span>


<span class="c">#---------------------------------------------------------------------------</span>

<span class="k">class</span> <span class="nc">ListSelectionRule</span><span class="p">(</span><span class="n">CompoundRule</span><span class="p">):</span>
    <span class="n">spec</span> <span class="o">=</span> <span class="s">&quot;list current selection&quot;</span>
    <span class="k">def</span> <span class="nf">_process_recognition</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">,</span> <span class="n">extras</span><span class="p">):</span>
        <span class="k">print</span> <span class="s">&quot;Current selection:&quot;</span>
        <span class="k">for</span> <span class="n">filename</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">grammar</span><span class="o">.</span><span class="n">get_selected_filenames</span><span class="p">():</span>
            <span class="k">print</span> <span class="s">&quot; - </span><span class="si">%r</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">filename</span>

<span class="n">grammar</span><span class="o">.</span><span class="n">add_rule</span><span class="p">(</span><span class="n">ListSelectionRule</span><span class="p">())</span>


<span class="c">#---------------------------------------------------------------------------</span>

<span class="k">class</span> <span class="nc">CommandRule</span><span class="p">(</span><span class="n">CompoundRule</span><span class="p">):</span>

    <span class="n">spec</span> <span class="o">=</span> <span class="s">&quot;[command] &lt;command&gt;&quot;</span>
    <span class="n">extras</span> <span class="o">=</span> <span class="p">[</span><span class="n">Choice</span><span class="p">(</span><span class="s">&quot;command&quot;</span><span class="p">,</span> <span class="nb">dict</span><span class="p">((</span><span class="n">c</span><span class="o">.</span><span class="n">spec</span><span class="p">,</span> <span class="n">c</span><span class="p">)</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">commands</span><span class="p">))]</span>

    <span class="k">def</span> <span class="nf">_process_recognition</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">,</span> <span class="n">extras</span><span class="p">):</span>
        <span class="n">command</span> <span class="o">=</span> <span class="n">extras</span><span class="p">[</span><span class="s">&quot;command&quot;</span><span class="p">]</span>
        <span class="n">paths</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">grammar</span><span class="o">.</span><span class="n">get_selected_paths</span><span class="p">()</span>
        <span class="n">directory</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">grammar</span><span class="o">.</span><span class="n">get_current_directory</span><span class="p">()</span>
        <span class="k">print</span> <span class="s">&quot;Selected paths: </span><span class="si">%r</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">paths</span>
        <span class="n">command</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="n">paths</span><span class="p">,</span> <span class="n">directory</span><span class="p">)</span>

<span class="n">grammar</span><span class="o">.</span><span class="n">add_rule</span><span class="p">(</span><span class="n">CommandRule</span><span class="p">())</span>


<span class="c">#---------------------------------------------------------------------------</span>
<span class="c"># Load the grammar instance and define how to unload it.</span>

<span class="n">grammar</span><span class="o">.</span><span class="n">load</span><span class="p">()</span>

<span class="c"># Unload function which will be called by natlink at unload time.</span>
<span class="k">def</span> <span class="nf">unload</span><span class="p">():</span>
    <span class="k">global</span> <span class="n">grammar</span>
    <span class="k">if</span> <span class="n">grammar</span><span class="p">:</span> <span class="n">grammar</span><span class="o">.</span><span class="n">unload</span><span class="p">()</span>
    <span class="n">grammar</span> <span class="o">=</span> <span class="bp">None</span>
</pre></div>
</td></tr></table></div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="index.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">Command-module to control <strong>Windows Explorer</strong></a><ul>
<li><a class="reference external" href="#installation">Installation</a></li>
<li><a class="reference external" href="#module-source-code">Module source code</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="mod-uedit32.html" title="previous chapter">Command-module for <strong>UltraEdit</strong> editor</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="mod-_word_styles.html" title="next chapter">Command-module for styles in <strong>Microsoft Word</strong></a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="_sources/mod-_explorer_tools.txt">Show Source</a></li>
            </ul>
          <h3>Quick search</h3>
            <form class="search" action="search.html" method="get">
              <input type="text" name="q" size="18" /> <input type="submit" value="Go" />
              <input type="hidden" name="check_keywords" value="yes" />
              <input type="hidden" name="area" value="default" />
            </form>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="mod-_word_styles.html" title="Command-module for styles in Microsoft Word"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="mod-uedit32.html" title="Command-module for UltraEdit editor"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">Dragonfly-modules v0.1 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; Copyright 2008, Christo Butcher.
      Last updated on Apr 10, 2009.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.5.1.
    </div>
  </body>
</html>